package app.fugema.shop.main.home.adapter;

import android.view.View;


import com.base.common.app.LoginUtils;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyGridLayoutManager;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;

import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import app.fugema.shop.BuildConfig;
import app.fugema.shop.R;
import app.fugema.shop.databinding.HomeGoodsCategoryItemBinding;
import app.fugema.shop.databinding.HomeGoodsCategoryItemItemBinding;

import app.fugema.shop.main.home.model.BannerData;
import app.fugema.shop.main.home.model.HomeData;
import app.fugema.shop.main.setting.view.WebActivity;


public class HomeGoodsCategoryBaseItemMultiType extends BaseItemMultiType<BannerData, HomeGoodsCategoryItemBinding>
{

    @Override
    public int getLayoutId()
    {
        return R.layout.home_goods_category_item;
    }

    @Override
    public int getItemViewType() {
        return TYPE_HEAD_ONE;
    }

    @Override
    public boolean isFullSpanType() {
        return true;
    }

    @Override
    public void onCreateViewHolder(HomeGoodsCategoryItemBinding binding, BaseViewHolder viewHolder)
    {
        super.onCreateViewHolder(binding, viewHolder);

        binding.rvRecyclerView.setLayoutManager(new MyGridLayoutManager(viewHolder.itemView.getContext(), 5));
        SingleRecyclerAdapter<HomeData.DatasetBean, HomeGoodsCategoryItemItemBinding> adapter = new SingleRecyclerAdapter<HomeData.DatasetBean, HomeGoodsCategoryItemItemBinding>() {
            @Override
            public int getLayoutId() {
                return R.layout.home_goods_category_item_item;
            }

            @Override
            public void onCreateViewHolder(HomeGoodsCategoryItemItemBinding binding, BaseViewHolder viewHolder) {
                super.onCreateViewHolder(binding, viewHolder);
                binding.clItemView.setOnClickListener(viewHolder);
            }


            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, HomeData.DatasetBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);

                switch ( bean.getLinkType() )
                {
                    case 8:
                        // 签到 大转盘 等 带上 token
                        StringBuffer tokensb = new StringBuffer();
                        if( UIUtils.isNotEmpty( LoginUtils.getToken() ) )
                        {
                            tokensb.append("?");
                            tokensb.append("token");
                            tokensb.append("=");
                            tokensb.append(LoginUtils.getToken());
                        }
                        String tokenStr = UIUtils.isEmpty(tokensb.toString())?"":tokensb.toString();
                        WebActivity.start(view.getContext() ,bean.getLink().trim() + tokenStr);
                        break;


                    default:
                        WebActivity.start(view.getContext() , BuildConfig.API_TEST + bean.getLink() );
                        break;
                }

                //UIUtils.showToastSafes("link = "+ bean.getLink() );
                LogUtil.e("首页link",bean.getLink());
                // 点击 head分类 item
                /*
                if (bean.getCategoryId() == 0)
                {
                    GoodsClassifyActivity.start(view.getContext());
                } else {
                    GoodsClassifyDetailActivity.start(view.getContext(), bean.getCategoryId(), bean.getCategoryName());
                }
                */
            }

        };
        binding.rvRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onBindViewHolder(HomeGoodsCategoryItemBinding binding, int position, BannerData bean)
    {
        super.onBindViewHolder(binding, position, bean);
        if (binding.rvRecyclerView.getAdapter() instanceof BaseRVAdapter)
        {
            BaseRVAdapter rvAdapter = (BaseRVAdapter) binding.rvRecyclerView.getAdapter();
            if (UIUtils.isNotEmpty(bean.getData()))
            {
                rvAdapter.setDataList(bean.getData());
            }
        }

    }


}
